﻿using EagleProject.LinQDataContext;
using EagleProject.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.WebControls;

namespace EagleProject.Controllers
{
    public class SubscriberController : MyController
    {
        //
        // GET: /Subscriber/Success
        public ActionResult Success()
        {
            if (!IsAuthenticated()) return RedirectToHome();

            return View();
        }

        //
        // GET: /Subscriber/
        public ActionResult Index()
        {
            if (!IsAuthenticated()) return RedirectToLogin("~/Subscriber/");

            return View();
        }

        private void SetViewBagBasedOnCardType(int cardType)
        {
            ViewBag.CardType = cardType;
            ViewBag.CommencementDate = String.Format("{0:MM/dd/yyyy}", DateTime.Now); ;
            ViewBag.IssueDate = String.Format("{0:MM/dd/yyyy}", DateTime.Now.AddDays(14));
            ViewBag.ExpiredDate = String.Format("{0:MM/dd/yyyy}", DateTime.Now.AddDays(14 + 365));

            switch (cardType)
            {
                case 1:
                    ViewBag.CardImage = "regularcard.png";
                    ViewBag.CardName = "Regular Card";
                    ViewBag.CardMoney = 1000;
                    break;
                case 2:
                    ViewBag.CardImage = "processregularcard.png";
                    ViewBag.CardName = "Process Regular Card";
                    ViewBag.CardMoney = 2000;
                    break;
                case 3:
                    ViewBag.CardImage = "specialcard.png";
                    ViewBag.CardName = "Special Card";
                    ViewBag.CardMoney = 3000;
                    break;
                case 4:
                    ViewBag.CardImage = "firstclasscard.png";
                    ViewBag.CardName = "First Class Card";
                    ViewBag.CardMoney = 7000;
                    break;
            }
        }

        //
        // GET: /Subscriber/RegisterForm/{cardType}
        public ActionResult RegisterForm(int? id)
        {
            if (!IsAuthenticated()) return RedirectToLogin(@"~/Subscriber/RegisterForm/" + (id.HasValue ? id : 1));

            ViewBag.ListState = new SelectList(DatabaseController.GetStates(), "ID", "Name");
            ViewBag.ListLGAs = new SelectList(DatabaseController.GetLGAs(), "ID", "Name");
            ViewBag.ListCountry = new SelectList(DatabaseController.GetCountries(), "ID", "Name");
            ViewBag.ListMarialStatus = ListMarialStatus;

            SetViewBagBasedOnCardType(id.Value);

            return View();
        }

        //
        // POST: /Subscriber/RegisterForm
        [HttpPost]
        public ActionResult RegisterForm(CommonSubscriberReg model)
        {
            if (!IsAuthenticated()) return RedirectToHome();

            int cardType = GetIntParam("CardType");
            if (cardType >= 1 && cardType <= 4)
            {
                model.CommonUser = GetUserInformation().ID;
                DatabaseController.CreateSubscriberReg(model, cardType);
                return RedirectToAction("Success");
            }
            return View();
        }

        // GET: /Subscriber/ListRegForm
        public ActionResult ListRegForm()
        {
            if (!IsAuthenticated()) return RedirectToHome();

            var data = DatabaseController.GetRegisterFormByMe(GetUserInformation().ID);

            return View(data);
        }

        //
        // GET: /Subscriber/EditRegForm/{id}
        public ActionResult EditRegForm(int? id)
        {
            if (!IsAuthenticated()) return RedirectToHome();
            if (!id.HasValue) return RedirectToHome();

            var data = DatabaseController.GetRegisterFormByMeById(GetUserInformation().ID, id.Value);
            if (data == null) return RedirectToAccessDenied();

            ViewBag.ListState = new SelectList(DatabaseController.GetStates(), "ID", "Name");
            ViewBag.ListLGAs = new SelectList(DatabaseController.GetLGAs(), "ID", "Name");
            ViewBag.ListCountry = new SelectList(DatabaseController.GetCountries(), "ID", "Name");
            ViewBag.ListMarialStatus = ListMarialStatus;

            SetViewBagBasedOnCardType(data.RegType.Value);

            return View(data);
        }

        //
        // POST: /Subscriber/EditRegForm
        [HttpPost]
        public ActionResult EditRegForm(CommonSubscriberReg model)
        {
            if (!IsAuthenticated()) return RedirectToHome();

            bool isSuccess = DatabaseController.EditRegisterForm(model, GetUserInformation().ID);

            if (isSuccess)
            {
                return RedirectToAction("Success");
            }
            return View(model);
        }


        //
        // GET: /Subscriber/DeleteRegForm/{id}
        public ActionResult DeleteRegForm(int? id)
        {
            if (!IsAuthenticated()) return RedirectToHome();
            if (!id.HasValue) return RedirectToHome();

            bool isSuccess = DatabaseController.DeleteRegterForm(id.Value, GetUserInformation().ID);
            if (isSuccess)
            {
                return RedirectToAction("Success");
            }
            return View();
        }

        //
        // GET: /Subscriber/DeleteRegForm/{id}
        public ActionResult ViewRegForm(int? id)
        {
            if (!IsAuthenticated()) return RedirectToHome();
            if (!id.HasValue) return RedirectToHome();

            var data = DatabaseController.GetRegisterFormById(id.Value);
            if (data.CommonUser.Value != GetUserInformation().ID) { return RedirectToAccessDenied(); }

            SetViewBagBasedOnCardType(data.RegType.Value);

            return View(data);
        }

        //
        // GET: /Subscriber/ViewRegForm/
        [HttpPost]
        public ActionResult ViewRegForm(CommonSubscriberReg model)
        {
            if (!IsAuthenticated()) return RedirectToHome();

            int id = model.ID;

            var CommencementDate = DateTime.Now;
            var IssueDate = CommencementDate.AddDays(14);
            var ExpiredDate = CommencementDate.AddDays(14 + 365);

            bool isSuccess = DatabaseController.EditRegisterForm(id,
                GetUserInformation().ID, CommencementDate, IssueDate, ExpiredDate);

            return RedirectToAction("Success");
        }
    }
}
